home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 476-500 / disk_498 / wordsearch / src / wsearch.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  5KB  |  255 lines

  1. #include <exec/types.h>
  2. #include <stdio.h>
  3. #include "wsearch.h"
  4.  
  5. void next();
  6.  
  7. char *wptr[MAXWORD];
  8. extern int LINK;
  9.  
  10.  
  11. int wsearch()
  12. {
  13.     int i,j,ls;
  14.     char *temp;
  15.  
  16.     for(i=0;i<MAXWORD;i++)
  17.         wptr[i] = word[i];
  18.  
  19.     ls=MAXWORD-1;
  20.     while(ls>0)
  21.     {
  22.         j=0;
  23.         for(i=0;i<ls;i++)
  24.         {
  25.             if(strlen(wptr[i])<strlen(wptr[i+1]))
  26.                 {
  27.                 j=i;
  28.                 temp = wptr[i];
  29.                 wptr[i] = wptr[i+1];
  30.                 wptr[i+1] = temp;
  31.                 }
  32.         }
  33.         ls = j;
  34.     }
  35.     
  36.     w=MAXWORD-1;
  37.          while((wptr[w])[0]==0 && w!=-1)
  38.             w--;
  39.          if(w==-1) return(0);
  40.  
  41.     open_requestor();
  42.  
  43.     i = setword(0);
  44.     
  45.     close_requestor();
  46.     
  47.     return(i);
  48. }
  49.  
  50. int setword(pos)
  51.  int pos;
  52.  {
  53.    char buffer[MAXSIZE+1];
  54.    int x,y,d,x0,y0,d0,s;
  55.    static int error,i;
  56.    int link;        /* indicate position must link with other words */
  57.  
  58.  
  59.    link = LINK;
  60.  
  61.    x = randint(px);
  62.    y = randint(py);
  63.    d = randint(7);
  64.    while((1<<d & filter) == 0)
  65.     {
  66.    d = d + 1;
  67.    if(d>7) d=0;
  68.     }
  69.  
  70.    s = 0;
  71.  
  72.    do
  73.     {
  74.       if(Stop_Flag()) return(-1);
  75.  
  76.       if(link==1)
  77.       {
  78.     error = advance(&x,&y,&d,pos,link);
  79.     if(error!=0)
  80.     {
  81.         link = 0;
  82.         error = advance(&x,&y,&d,pos,link);
  83.     }
  84.       }
  85.       else error = advance(&x,&y,&d,pos,link);
  86.       
  87.       if(error!=0) return(1);
  88.       if(s==0)
  89.        {
  90.          s = 1;
  91.          x0 = x;
  92.          y0 = y;
  93.          d0 = d;
  94.        }
  95.  
  96.       buffer[strlen((wptr[pos]))] = NULL;
  97.       for(i=0;i<strlen((wptr[pos]));i++)
  98.        {
  99.          if(d==0)
  100.           {
  101.             buffer[i]=Key(x+i,y);
  102.             Key(x+i,y)=(wptr[pos])[i];
  103.           }
  104.          else if(d==1)
  105.           {
  106.             buffer[i]=Key(x+i,y+i);
  107.             Key(x+i,y+i)=(wptr[pos])[i];
  108.           }
  109.          else if(d==2)
  110.           {
  111.             buffer[i]=Key(x,y+i);
  112.             Key(x,y+i)=(wptr[pos])[i];
  113.           }
  114.          else if(d==3)
  115.           {
  116.             buffer[i]=Key(x-i,y+i);
  117.             Key(x-i,y+i)=(wptr[pos])[i];
  118.           }
  119.          else if(d==4)
  120.           {
  121.             buffer[i]=Key(x-i,y);
  122.             Key(x-i,y)=(wptr[pos])[i];
  123.           }
  124.          else if(d==5)
  125.           {
  126.             buffer[i]=Key(x-i,y-i);
  127.             Key(x-i,y-i)=(wptr[pos])[i];
  128.           }
  129.          else if(d==6)
  130.           {
  131.             buffer[i]=Key(x,y-i);
  132.             Key(x,y-i)=(wptr[pos])[i];
  133.           }
  134.          else if(d==7)
  135.           {
  136.             buffer[i]=Key(x+i,y-i);
  137.             Key(x+i,y-i)=(wptr[pos])[i];
  138.           }
  139.        }
  140.  
  141.       if(pos<w)
  142.          error=setword(pos+1);
  143.       else error = 0;
  144.  
  145.       if(error==0) return(0);
  146.  
  147.       for(i=0;i<strlen((wptr[pos]));i++)
  148.        {
  149.          if(d==0)
  150.             Key(x+i,y)=buffer[i];
  151.          else if(d==1)
  152.             Key(x+i,y+i)=buffer[i];
  153.          else if(d==2)
  154.             Key(x,y+i)=buffer[i];
  155.          else if(d==3)
  156.             Key(x-i,y+i)=buffer[i];
  157.          else if(d==4)
  158.             Key(x-i,y)=buffer[i];
  159.          else if(d==5)
  160.             Key(x-i,y-i)=buffer[i];
  161.          else if(d==6)
  162.             Key(x,y-i)=buffer[i];
  163.          else if(d==7)
  164.             Key(x+i,y-i)=buffer[i];
  165.        }
  166.       next(&x,&y,&d);
  167.       advance(&x,&y,&d,pos);
  168.       if(x==x0 && y==y0 && d==d0) return(1);
  169.       
  170.       s = 1;
  171.     }
  172.     while(error!=0);
  173.     
  174.     return(0);
  175.  }
  176.  
  177. void next(x,y,d)
  178.  int *x,*y,*d;
  179.  {
  180.    *x = *x + 1;
  181.         if(*x>px)
  182.          {
  183.            *x = 0;
  184.            *y = *y + 1;
  185.          }
  186.         if(*y>py)
  187.          {
  188.            *x = 0;
  189.            *y = 0;
  190.       *d = *d + 1;
  191.          }
  192.         if(*d>7)
  193.          {
  194.            *x = 0;
  195.            *y = 0;
  196.            *d = 0;
  197.          }
  198.    while((1<<*d & filter) == 0)
  199.        {
  200.       *d = *d + 1;
  201.       if(*d>7) *d=0;
  202.        }
  203.  }
  204.  
  205. int advance(x,y,d,pos,link)
  206.  int *x,*y,*d,pos,link;
  207.  {
  208.    int x0,y0,d0,i,x1,y1;
  209.  
  210.    x0=*x;y0=*y;d0=*d;
  211.  
  212.    for(i=0;i<strlen(wptr[pos]);)
  213.     {
  214.       if(*d==0||*d==1||*d==7)
  215.          x1=*x+i;
  216.       else if(*d==3||*d==4||*d==5)
  217.          x1=*x-i;
  218.       else
  219.          x1=*x;
  220.  
  221.       if(*d==1||*d==2||*d==3)
  222.          y1=*y+i;
  223.       else if(*d==5||*d==6||*d==7)
  224.          y1=*y-i;
  225.       else
  226.          y1=*y;
  227.  
  228.       if((Key(x1,y1)!=' ' && (wptr[pos])[i]!=Key(x1,y1))||
  229.          x1<0 || x1>px || y1<0 || y1>py)
  230.        {
  231.          i = 0;
  232.      if(link!=0) link=1;
  233.          next(x,y,d);
  234.          if(x0==*x && y0==*y && d0==*d) return(1);
  235.        }
  236.       else
  237.       {
  238.      if(link==1 && (wptr[pos])[i]==Key(x1,y1)) link = -1;
  239.      /* the link is used to indicate both if a link was requested (not 0)
  240.         and if the current position does link (-1)
  241.       */
  242.          i = i + 1;
  243.       }
  244.  
  245.       if(link==1 && i==strlen(wptr[pos]))    /* finished word but no link */
  246.       {
  247.      i = 0;
  248.          next(x,y,d);
  249.          if(x0==*x && y0==*y && d0==*d) return(1);
  250.       }
  251.     }
  252.  
  253.    return(0);
  254.  }
  255.